package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.os.Parcelable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.ShopSortType;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by shq1 on 2017/10/12.
 * 单选类型适配器
 */

public class TypeChoseAdapter extends RecyclerView.Adapter<TypeChoseAdapter.ViewHolder> {
    private Context context;

    public List<ShopSortType> getmDataList() {
        return mDataList;
    }

    public void setmDataList(List<ShopSortType> mDataList) {
        this.mDataList = mDataList;
    }

    private List<ShopSortType> mDataList;
    private OnTypeItemCheckListener mOnClick;

    public TypeChoseAdapter(Context mContext, List<ShopSortType> list, OnTypeItemCheckListener onClick) {
        this.context = mContext;
        this.mDataList = list;
        this.mOnClick=onClick;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_typechose, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tvTypename.setText(mDataList.get(position).getName());
        holder.check.setChecked(mDataList.get(position).isChoose());
        holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                setChecks(position);
                mOnClick.onItemChecked(position);
            }
        });
    }
    private void setChecks(int position){
        for (int i=0;i<mDataList.size();i++) {
            ShopSortType type=mDataList.get(i);
            if(position==i){
                type.setChoose(true);
            }else{
                type.setChoose(false);
            }
        }
    }
    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_typename)
        TextView tvTypename;
        @BindView(R.id.check)
        CheckBox check;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
    public interface OnTypeItemCheckListener{
        void onItemChecked(int position);
    }

}
